package com.cyan.app.workflow.activity.SFC;

import android.content.Context;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.cyan.app.workflow.AppContext;
import com.cyan.app.workflow.R;
import com.cyan.app.workflow.base.activity.BaseActivity;
import com.cyan.app.workflow.bean.SalaryBean;
import com.cyan.app.workflow.common.HttpConfig;
import com.cyan.app.workflow.customview.ShowTipDialog;
import com.cyan.app.workflow.util.network.GetRequestParams;
import com.cyan.app.workflow.widget.PullToRefresh.PullToRefreshListView;
import com.alibaba.fastjson.annotation.JSONField;
import com.google.gson.Gson;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by DXW on 2017/1/6.
 * 我的物流详情页
 */

public class DDDataDetail extends BaseActivity {
    private SalaryBean salaryBean;
    private String userno;
    private String date;
    private ShowTipDialog mShowTipDialog = new ShowTipDialog();
    private ListView mShowListView;
    private PullToRefreshListView mPullToRefreshExpandableListView;
    private PullToRefreshListView mPullToRefreshNoDataListView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sfc_dd);
        userno = getIntent().getStringExtra("userno");
        date = getIntent().getStringExtra("date");
        LinearLayout sfc_dd_lin =(LinearLayout)findViewById(R.id.sfc_dd_lin);
        sfc_dd_lin.setVisibility(View.GONE);
        LinearLayout sfc_dd_title =(LinearLayout)findViewById(R.id.sfd_dd_title);
        sfc_dd_title.setVisibility(View.GONE);

        mPullToRefreshNoDataListView = (PullToRefreshListView) findViewById(R.id.no_match_show);
        mPullToRefreshNoDataListView.setVisibility(View.GONE);
        ListView listView = mPullToRefreshNoDataListView.getRefreshableView();
        listView.setAdapter(mNoDataAdapter);
        mPullToRefreshExpandableListView = (PullToRefreshListView) findViewById(R.id.match_show_list);
        mShowListView = mPullToRefreshExpandableListView.getRefreshableView();
        mPullToRefreshExpandableListView.setPullToRefreshEnabled(false);
        titleBar();
        new AsyncLoader().execute();
    }
    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //MobclickAgent.onPause(this);
        //MobclickAgent.onPageEnd("DDActivity");
    }

    @Override
    protected void onResume() {
        super.onResume();
        //MobclickAgent.onResume(this);
        //MobclickAgent.onPageStart("DDActivity");
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }
    private void titleBar() {
        TextView mTitleName = (TextView) findViewById(R.id.top_title_info);
        mTitleName.setText("物流明细 " + date);
        Button mButton = (Button) findViewById(R.id.top_title_refres_with_icon);
        mButton.setVisibility(View.INVISIBLE);
        mButton = (Button) findViewById(R.id.top_title_refres);
        mButton.setVisibility(View.INVISIBLE);
        Button back = (Button) findViewById(R.id.top_title_back);
        back.getBackground().setAlpha(255);// 透明度0~255透明度值 ，值越小越透明
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //返回按钮
                finish();
            }
        });
    }

    private void onRefreshComplete() {
        if (mPullToRefreshExpandableListView.isRefreshing()) {
            mPullToRefreshExpandableListView.onRefreshComplete();
        }

        if (mPullToRefreshNoDataListView.isRefreshing()) {
            mPullToRefreshNoDataListView.onRefreshComplete();
        }
    }
    //没数据处理
    BaseAdapter mNoDataAdapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return 1;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView != null) {
                return convertView;
            }
            convertView = LayoutInflater.from(DDDataDetail.this).inflate(R.layout.layout_main_nodata, null);
            return convertView;
        }
    };
    private class AsyncLoader extends AsyncTask<String, Integer, String> {
        @Override
        protected void onPreExecute() {
            mShowTipDialog.showDialog(DDDataDetail.this);
        }

        @Override
        protected String doInBackground(String... params) {
            String result = null;
            try{
                JSONObject param = new JSONObject();
                param.put("userno", userno);
                param.put("date", date);
                result= GetRequestParams.getRequestResult(HttpConfig.HTTP_MAIN_SERVER_URL, "225", param.toString(), getApplicationContext());
            }catch (Exception e){
                Log.e("error", e.toString());
            }
            return result;
        }

        @Override
        protected void onPostExecute(String s) {
            DDPoolpdListBean commonBean = new Gson().fromJson(s,DDPoolpdListBean.class);
            if(commonBean!=null&&commonBean.getResult().size()>0){
                //显示
                DDDataDetailAdapter adapter = new DDDataDetailAdapter();
                mShowListView.setAdapter(adapter);
                adapter.setDatas(commonBean.getResult());
                adapter.notifyDataSetChanged();
            }
            onRefreshComplete();
            mShowTipDialog.closeDialog();
        }
    }
    private class DDDataDetailAdapter extends BaseAdapter {
        List<DDPoolpdListBean.DDPoolpdBean> mpoolpdList;
        private LayoutInflater mInflater ;
        public DDDataDetailAdapter() {
            mInflater = (LayoutInflater) AppContext.getInstance().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public void setDatas(List<DDPoolpdListBean.DDPoolpdBean> poolpdList) {
            if (poolpdList != null) {
                mpoolpdList = poolpdList;
            }
        }
        @Override
        public int getCount() {
            return mpoolpdList==null?0:mpoolpdList.size();
        }

        @Override
        public Object getItem(int position) {
            return mpoolpdList==null?null:mpoolpdList.get(position);
        }
        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder viewHolder;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.sfc_dd_tab22, null);
                viewHolder = new ViewHolder();
                viewHolder.ProssName = (TextView) convertView.findViewById(R.id.dd_tab22_prosscode);
                viewHolder.Date = (TextView) convertView.findViewById(R.id.dd_tab22_lzdate);
                viewHolder.Itnbr = (TextView) convertView.findViewById(R.id.dd_tab22_itnbr);
                viewHolder.Manno = (TextView) convertView.findViewById(R.id.dd_tab22_manno);
                viewHolder.Onhand1 = (TextView) convertView.findViewById(R.id.dd_tab22_onhand1);
                viewHolder.NextPross = (TextView) convertView.findViewById(R.id.dd_tab22_nextpross);
                viewHolder.Distance = (TextView) convertView.findViewById(R.id.dd_tab22_distance);
                viewHolder.Amount = (TextView) convertView.findViewById(R.id.dd_tab22_amount);
                viewHolder.Type = (TextView) convertView.findViewById(R.id.dd_tab22_type);
                convertView.setTag(viewHolder);
            }else {
                viewHolder = (ViewHolder)convertView.getTag();
            }
            viewHolder.Date.setText(mpoolpdList.get(position).getLzTime());
            viewHolder.ProssName.setText(mpoolpdList.get(position).getProssName());
            viewHolder.Itnbr.setText(mpoolpdList.get(position).getItnbr());
            viewHolder.Manno.setText(mpoolpdList.get(position).getManno());
            viewHolder.Onhand1.setText(mpoolpdList.get(position).getOnhand1().toString());

            viewHolder.NextPross.setText(mpoolpdList.get(position).getNextProssName());
            double distance =Double.parseDouble(mpoolpdList.get(position).getDistance());
            viewHolder.Distance.setText(String.valueOf((int)distance));
            double amount =Double.parseDouble(mpoolpdList.get(position).getAmount());
            viewHolder.Amount.setText(String.valueOf((int)amount));
            if(mpoolpdList.get(position).getType()==1){
                viewHolder.Type.setText("良品");
                viewHolder.Onhand1.setTextColor(Color.parseColor("#60ca82"));
            }else if(mpoolpdList.get(position).getType()==2){
                viewHolder.Type.setText("不良返修");
                viewHolder.Onhand1.setTextColor(Color.parseColor("#4a86e8"));
            }else if(mpoolpdList.get(position).getType()==3){
                viewHolder.Type.setText("报废品");
                viewHolder.Onhand1.setTextColor(Color.parseColor("#fe5052"));
            }
            return convertView;
        }
        class ViewHolder {
            protected TextView ProssName;
            protected TextView Date;
            protected TextView Manno;
            protected TextView Itnbr;
            protected TextView Onhand1;

            protected TextView NextPross;
            protected TextView Distance;
            protected TextView Amount;
            protected TextView Type;
        }
    }

    private class DDPoolpdListBean {
        @JSONField(name="Result")
        List<DDPoolpdListBean.DDPoolpdBean> Result;

        @JSONField(name="TimeStamp")
        private long timeStamp;

        public DDPoolpdListBean() {
            Result = new ArrayList<DDPoolpdBean>();
        }
        public List<DDPoolpdListBean.DDPoolpdBean> getResult() {
            return Result;
        }
        public void setDDPools(List<DDPoolpdBean> ddPools) {
            this.Result = ddPools;
        }

        public long getTimeStamp() {
            return timeStamp;
        }
        public void setTimeStamp(long timeStamp) {
            this.timeStamp = timeStamp;
        }

        class DDPoolpdBean {
            private String PoolId;
            private String Manno;
            private String ProssCode;
            private String ProssName;
            private String Itnbr;
            private Double Onhand1;

            private int Type;
            private String Keyindate;
            private String Username;
            private String Userno;
            private String WlTime;
            private String WlUserno;
            private String WlUsername;
            private String QdTime;
            private String NextPross;
            private String NextProssName;
            private String LzTime;
            private String Tofixnr;
            private String Distance;
            private String Amount;

            public String getAmount() {
                return Amount;
            }

            public void setAmount(String amount) {
                Amount = amount;
            }

            public String getLzTime() {
                return LzTime;
            }

            public void setLzTime(String lzTime) {
                LzTime = lzTime;
            }

            public String getTofixnr() {
                return Tofixnr;
            }

            public void setTofixnr(String tofixnr) {
                Tofixnr = tofixnr;
            }

            public String getDistance() {
                return Distance;
            }

            public void setDistance(String distance) {
                Distance = distance;
            }

            public String getPoolId() {
                return PoolId;
            }

            public void setPoolId(String poolId) {
                PoolId = poolId;
            }

            public String getManno() {
                return Manno;
            }

            public void setManno(String manno) {
                Manno = manno;
            }

            public String getProssName() {
                return ProssName;
            }

            public void setProssName(String prossName) {
                ProssCode = prossName;
            }

            public String getProssCode() {
                return ProssCode;
            }

            public void setProssCode(String prossCode) {
                ProssCode = prossCode;
            }

            public String getItnbr() {
                return Itnbr;
            }

            public void setItnbr(String itnbr) {
                Itnbr = itnbr;
            }

            public Double getOnhand1() {
                return Onhand1;
            }

            public void setOnhand1(Double onhand1) {
                Onhand1 = onhand1;
            }

            public int getType() {
                return Type;
            }

            public void setType(int type) {
                Type = type;
            }

            public String getKeyindate() {
                return Keyindate;
            }

            public void setKeyindate(String keyindate) {
                Keyindate = keyindate;
            }

            public String getUserno() {
                return Userno;
            }

            public void setUserno(String userno) {
                Userno = userno;
            }

            public String getWlTime() {
                return WlTime;
            }

            public void setWlTime(String wlTime) {
                WlTime = wlTime;
            }

            public String getWlUserno() {
                return WlUserno;
            }

            public void setWlUserno(String wlUserno) {
                WlUserno = wlUserno;
            }

            public String getQdTime() {
                return QdTime;
            }

            public void setQdTime(String qdTime) {
                QdTime = qdTime;
            }

            public String getNextPross() {
                return NextPross;
            }

            public void setNextPross(String nextPross) {
                NextPross = nextPross;
            }

            public String getNextProssName() {
                return NextProssName;
            }

            public void setNextProssName(String nextProssName) {
                NextProssName = nextProssName;
            }

            public String getUsername() {
                return Username;
            }

            public void setUsername(String username) {
                Username = username;
            }

            public String getWlUsername() {
                return WlUsername;
            }

            public void setWlUsername(String wlUsername) {
                WlUsername = wlUsername;
            }
        }
    }
}
